clear;

%%%%%%%%%%%%%%%%%%%%%%Data%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data = dataset('File','table1_3_data.csv','Delimiter',',');

iter = 1;
N = 615;
warning('off','all');
stations = [data.station_id1, data.station_id2, data.station_id3];

y1 = data.voteshare_vb_2014;
X10 = [ones(N,1)];
X11 = [ones(N,1) data.poll_date1 data.poll_date2 data.poll_date3 data.poll_date4];
X12 = [ones(N,1) data.voteshare_vb_2009 data.poll_date1 data.poll_date2 data.poll_date3 data.poll_date4];
X13 = [ones(N,1) data.voteshare_vb_2009 data.poll_date1 data.poll_date2 data.poll_date3 data.poll_date4 data.lit_pc data.rural_pc data.scst_pc];
X14 = [ones(N,1) data.voteshare_vb_2009 data.poll_date1 data.poll_date2 data.poll_date3 data.poll_date4 data.lit_pc data.rural_pc data.scst_pc data.state_election];
X15 = [ones(N,1) data.voteshare_vb_2009 data.poll_date1 data.poll_date2 data.poll_date3 data.poll_date4 data.lit_pc data.rural_pc data.scst_pc data.state_election data.num_resp];

idx1 = find(~isnan(y1));

[beta,se,p,r2,F,control_mean,N,C,T] = ipwreg(y1(idx1),data.treat(idx1),X10(idx1,:),data.wgt_treat(idx1),stations(idx1,:),2);
p_ri = ri(iter,beta,se,y1(idx1),X10(idx1,:),stations(idx1,:),2,data.prob_treat(idx1));
results1(:,1) = [beta(1);se(1);p(1);p_ri(1);r2;control_mean;N];

[beta,se,p,r2,F,control_mean,N,C,T] = ipwreg(y1(idx1),data.treat(idx1),X11(idx1,:),data.wgt_treat(idx1),stations(idx1,:),2);
p_ri = ri(iter,beta,se,y1(idx1),X11(idx1,:),stations(idx1,:),2,data.prob_treat(idx1));
results1(:,2) = [beta(1);se(1);p(1);p_ri(1);r2;control_mean;N];

[beta,se,p,r2,F,control_mean,N,C,T] = ipwreg(y1(idx1),data.treat(idx1),X12(idx1,:),data.wgt_treat(idx1),stations(idx1,:),2);
p_ri = ri(iter,beta,se,y1(idx1),X12(idx1,:),stations(idx1,:),2,data.prob_treat(idx1));
results1(:,3) = [beta(1);se(1);p(1);p_ri(1);r2;control_mean;N];

[beta,se,p,r2,F,control_mean,N,C,T] = ipwreg(y1(idx1),data.treat(idx1),X13(idx1,:),data.wgt_treat(idx1),stations(idx1,:),2);
p_ri = ri(iter,beta,se,y1(idx1),X13(idx1,:),stations(idx1,:),2,data.prob_treat(idx1));
results1(:,4) = [beta(1);se(1);p(1);p_ri(1);r2;control_mean;N];

[beta,se,p,r2,F,control_mean,N,C,T] = ipwreg(y1(idx1),data.treat(idx1),X14(idx1,:),data.wgt_treat(idx1),stations(idx1,:),2);
p_ri = ri(iter,beta,se,y1(idx1),X14(idx1,:),stations(idx1,:),2,data.prob_treat(idx1));
results1(:,5) = [beta(1);se(1);p(1);p_ri(1);r2;control_mean;N];

[beta,se,p,r2,F,control_mean,N,C,T] = ipwreg(y1(idx1),data.treat(idx1),X15(idx1,:),data.wgt_treat(idx1),stations(idx1,:),2);
p_ri = ri(iter,beta,se,y1(idx1),X15(idx1,:),stations(idx1,:),2,data.prob_treat(idx1));
results1(:,6) = [beta(1);se(1);p(1);p_ri(1);r2;control_mean;N];

format bank;
disp(results1);

N = 615;
y5 = data.turnoutrate_2014;
idx5 = find(~isnan(y5));

X50 = [ones(N,1)];
X51 = [ones(N,1) data.poll_date1 data.poll_date2 data.poll_date3 data.poll_date4];
X52 = [ones(N,1) data.turnoutrate_2009 data.poll_date1 data.poll_date2 data.poll_date3 data.poll_date4];
X53 = [ones(N,1) data.turnoutrate_2009 data.poll_date1 data.poll_date2 data.poll_date3 data.poll_date4 data.lit_pc data.rural_pc data.scst_pc];
X54 = [ones(N,1) data.turnoutrate_2009 data.poll_date1 data.poll_date2 data.poll_date3 data.poll_date4 data.lit_pc data.rural_pc data.scst_pc data.state_election];

[beta,se,p,r2,F,control_mean,N,C,T] = ipwreg(y5(idx5),data.treat(idx5),X50(idx5,:),data.wgt_treat(idx5),stations(idx5,:),2);
p_ri = ri(iter,beta,se,y5(idx5),X50(idx5),stations(idx5,:),2,data.prob_treat(idx5));
results2(:,1) = [beta(1);se(1);p(1);p_ri(1);r2;control_mean;N];

[beta,se,p,r2,F,control_mean,N,C,T] = ipwreg(y5(idx5),data.treat(idx5),X51(idx5,:),data.wgt_treat(idx5),stations(idx5,:),2);
p_ri = ri(iter,beta,se,y5(idx5),X51(idx5),stations(idx5,:),2,data.prob_treat(idx5));
results2(:,2) = [beta(1);se(1);p(1);p_ri(1);r2;control_mean;N];

[beta,se,p,r2,F,control_mean,N,C,T] = ipwreg(y5(idx5),data.treat(idx5),X52(idx5,:),data.wgt_treat(idx5),stations(idx5,:),2);
p_ri = ri(iter,beta,se,y5(idx5),X52(idx5),stations(idx5,:),2,data.prob_treat(idx5));
results2(:,3) = [beta(1);se(1);p(1);p_ri(1);r2;control_mean;N];

[beta,se,p,r2,F,control_mean,N,C,T] = ipwreg(y5(idx5),data.treat(idx5),X53(idx5,:),data.wgt_treat(idx5),stations(idx5,:),2);
p_ri = ri(iter,beta,se,y5(idx5),X53(idx5),stations(idx5,:),2,data.prob_treat(idx5));
results2(:,4) = [beta(1);se(1);p(1);p_ri(1);r2;control_mean;N];

[beta,se,p,r2,F,control_mean,N,C,T] = ipwreg(y5(idx5),data.treat(idx5),X54(idx5,:),data.wgt_treat(idx5),stations(idx5,:),2);
p_ri = ri(iter,beta,se,y5(idx5),X54(idx5),stations(idx5,:),2,data.prob_treat(idx5));
results2(:,5) = [beta(1);se(1);p(1);p_ri(1);r2;control_mean;N];

format bank;
disp(results2);
